package com.admin.controller;


import com.admin.entity.Admin;
import com.admin.entity.StationKeyRecharge;
import com.admin.entity.StationRecharge;
import com.admin.service.IStationKeyRechargeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author marisheng
 * @since 2019-11-21
 */
@RestController
@RequestMapping("/admin/station-key-recharge")
public class StationKeyRechargeController {

    @Autowired
    private IStationKeyRechargeService stationKeyRechargeService;

    @Autowired
    private  HttpServletRequest request;

    /**
     * 获取站点秘钥记录
     * @param page
     * @param limit
     * @param map
     * @return
     */
    @GetMapping("/queryKeyRecharge")
    public ResponseBean queryKeyRecharge(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int limit, @RequestParam Map map){

        Admin admin = (Admin)request.getSession().getAttribute("admin");
        if (admin==null)
        {
            return new ResponseBean(401, "没有登录", null);
        }

        String stationId= (String) map.get("stationId");
        if (stationId==null||stationId==""){
            return new ResponseBean(500,"查询失败","未获取到站点Id");
        }

        IPage iPage = stationKeyRechargeService.queryKeyRecharge(page,limit,map);

        return new ResponseBean(0, iPage.getTotal(), iPage.getRecords());
    }
    /**
     * 站点密钥充值
     * @param skr
     * @return
     */
    @PostMapping("/insertTerminalKey")
    public ResponseBean insertTerminalKey(StationKeyRecharge skr) {

        Admin admin = (Admin)request.getSession().getAttribute("admin");
        if (admin==null)
        {
            return new ResponseBean(401, "没有登录", null);
        }

        //往充值密钥的交易记录表新增数据以及修改站点的密钥的数量并且循环在密钥表里加数据
        int n =stationKeyRechargeService.insertTerminalKeys(skr);
        return new ResponseBean(0, "", "充值成功");
    }
    /**
     * 站点密钥充值记录
     * @param page
     * @param limit
     * @param map
     * @return
     */
    @GetMapping("/StationKeyRecharge")
    public ResponseBean StationKeyRecharge(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, @RequestParam Map map) {
        Admin admin = (Admin)request.getSession().getAttribute("admin");
        if (admin==null)
        {
            return new ResponseBean(401, "没有登录", null);
        }

        IPage p = stationKeyRechargeService.selectKeyRecharge(page, limit, map);
        return new ResponseBean(0, p.getTotal(), p.getRecords());
    }
}
